/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ToolFragment.java   
 * @Package com.jiyi.activity.new_maintenance   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-31 下午3:40:20   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.activity.escape.OwnerListWithMapActivity;
import com.jiyi.activity.new_maintenance.repair.MaintenanceRepairListActivity;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.TaskCountEvent;
import com.jiyi.inteface.WebApi;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ToolFragment
 * @Description:TODO(工具)
 * @author: HYM
 * @date: 2017-8-31 下午3:40:20
 * 
 */
public class ToolWbNormalFragment extends Fragment {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;
	
	@BindView(R.id.tvTaskCount)
	TextView tvTaskCount;

	int wbTask = 0;
	int bxTask = 0;
			
	
	LayoutInflater inflater;
	ViewGroup container;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tool_wb_normal_menu, null, false);
		ButterKnife.bind(this, view);
		this.inflater = inflater;
		this.container = container;
		init();
		return view;
	}

	private void init() {
		setTop();
		getData();
		EventBus.getDefault().register(this);
	}
	
	@Subscribe
	public void onTaskCountEvent(TaskCountEvent taskCountEvent)
	{
		getData();
	}
	
	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());

		VolleyRequest.StringRequestPost(getContext(),
				WebApi.MAINTENANCE_TASK_COUNT, "", params, new StringInterface(
						getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								String taskCount = new JSONObject(data).getString("taskCount");
								tvTaskCount.setText("("+taskCount+")");
								wbTask = new JSONObject(data).getInt("wbTask");
								bxTask = new JSONObject(data).getInt("bxTask");
							} 
							else if (status.equals("404") && info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg("尚未登录或该用户在其他地方登录，请重新登录！");
								//清除环信登录
								UserHelp.getInstance().clearUser(AppBase.Context, true);
								return;
							}
							else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {

						}
					}
					
					@Override
					public void stringError(VolleyError error) {

					}
				});
	}

	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-8-31
	 */
	private void setTop() {
		tvBack.setVisibility(View.GONE);
		tvTitle.setText("维保管理系统");
		tvSearch.setVisibility(View.GONE);
	}

	@OnClick({ R.id.rlMyTask, R.id.tvMaintenance, R.id.tvRepair, R.id.tvOwner,
			R.id.tvEscape })
	public void onClick(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.rlMyTask:
			intent = new Intent(getContext(), MyTaskActivity.class);
			intent.putExtra("wbTaskCount", wbTask);
			intent.putExtra("wxTaskCount", bxTask);
			startActivity(intent);
			break;
		case R.id.tvMaintenance:
			intent = new Intent(getContext(), MaintenanceTaskActivity.class);
			startActivity(intent);
			break;
		case R.id.tvRepair:
			intent = new Intent(getContext(), MaintenanceRepairListActivity.class);
//			intent = new Intent(getContext(), RepairTaskActivity.class);
			startActivity(intent);
			break;
		case R.id.tvOwner:
			intent = new Intent(getContext(), OwnerListActivity.class);
			startActivity(intent);
			break;
		case R.id.tvEscape:
			intent = new Intent(getContext(), OwnerListWithMapActivity.class);
			startActivity(intent);
			break;
		
		default:
			break;
		}
	}
}
